package com.simou.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.Gson;
import com.simou.common.R;
import com.simou.entity.User;
import com.simou.service.UserService;
import com.simou.utils.SMSUtils;
import com.simou.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    StringRedisTemplate redisTemplate;

    // 发送手机验证码
    @RequestMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        log.info("用户为:{}",user);
        // 获取手机号
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            // 生成验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码为:{}",code);

            // 调用阿里云api发送短信
//            SMSUtils.sendMessage("阿里云短信测试","SMS_154950909",phone,code);

            // session保存验证码
//            session.setAttribute("code",code);
            // redis保存验证码
            redisTemplate.opsForValue().set("code",code,5, TimeUnit.MINUTES);
            return R.success("发送验证码成功");
        }

        return R.error("手机号为空");
    }

    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession session) {
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();

        // 获取session中存储的验证码
//        Object codeInSession = session.getAttribute("code");
        // 获取redis中存储的验证码
        String codeInRedis = redisTemplate.opsForValue().get("code");


        if(codeInRedis != null && codeInRedis.equals(code)) {
//        if(codeInSession != null && codeInSession.equals(code)) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);

            User user = userService.getOne(queryWrapper);

            if(user == null){
                // 当前手机号对应用户是新用户,自动注册到数据库
                user = new User();
                user.setPhone(phone);
                user.setStatus(1L);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());

            // 登录成功，删除redis缓存中的验证码
            redisTemplate.delete("code");
            return R.success(user);
        }
        return R.error("登录失败");
    }


}
